package me.zhengjie.modules.quartz.task.onlyOnce;

import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.quartz.service.dto.SyncCommon;
import me.zhengjie.modules.sync.pull.EstateSync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: youqing.yang
 * @Create: 2021/03/03 17:11
 * @Description:
 **/
@Slf4j
@Component
public class LCEstateDeveloperTask {

    @Autowired
    private EstateSync estateSync;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void run() {
        //仅执行绿城两个库
        Query queryParam = new Query();
        Criteria criteriaParam = new Criteria();
        criteriaParam.orOperator(Criteria.where("corporationCode").is("sh10417"), Criteria.where("corporationCode").is("sh10416"));
        queryParam.addCriteria(criteriaParam);
        List<Corporation> corporations = mongoTemplate.find(queryParam, Corporation.class);

        corporations.forEach(item -> {
            String corporationCode = item.getCorporationCode();
            if (StringUtil.isEmpty(item.getPullDomain())) {
                log.error("集团:{} 的同步数据域名未配置", corporationCode);
                return;
            }
            Criteria criteria = Criteria.where("corporationCode").is(corporationCode).and("status").is(1);
            List<CityCompany> companies = mongoTemplate.find(new Query(criteria), CityCompany.class);
            companies.forEach(company -> {
                SyncCommon syncCommon = new SyncCommon();
                syncCommon.setCorporationCode(item.getCorporationCode());
                syncCommon.setAppId(item.getPullAppId());
                syncCommon.setAppKey(item.getPullAppKey());
                syncCommon.setDomain(item.getPullDomain());
                syncCommon.setCityCode(company.getCityCode());
                syncCommon.setCompanyUuid(company.getCompanyUuid());
                syncCommon.setInit(true);
                log.info("开始处理公司:{}的楼盘开发商字段数据，开始时间:{}", company.getCompanyUuid(), new Date());
                estateSync.pull(syncCommon, null);
                log.info("完成处理公司:{}的楼盘开发商字段数据，结束时间:{}", company.getCompanyUuid(), new Date());
            });
        });
    }
}
